﻿/*
 * Copyright 2010-2013 Team Robot, Inc.
 * 
 * Author: Peter Coley
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Xml.Linq;

namespace TeamRobot
{
    public static class Utility
    {
        /// <summary>
        /// Enum utility methods that are a bit easier to use than the built-in ones.
        /// </summary>
        public static class Enum
        {
            public static int GetLength<T>()
            {
                Type enumType = typeof(T);
                return enumType.GetFields(BindingFlags.Public | BindingFlags.Static).Length;
            }

            public static string[] GetNames<T>()
            {
                Type enumType = typeof(T);
                return enumType.GetFields(BindingFlags.Public | BindingFlags.Static).Select(f => f.Name).ToArray();
            }

            public static T[] GetValues<T>()
            {
                Type enumType = typeof(T);
                return enumType.GetFields(BindingFlags.Public | BindingFlags.Static).Select(f => (T)f.GetValue(enumType)).ToArray();
            }

            public static T Parse<T>(string Name)
                where T : struct
            {
                return (T)System.Enum.Parse(typeof(T), Name, true);
            }

            /// <summary>
            /// Convenience to parse an enum value from an XElement attribute.
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="x"></param>
            /// <param name="attrName"></param>
            /// <returns></returns>
            public static T Parse<T>(XElement x, string attrName)
            {
                return (T)System.Enum.Parse(typeof(T), (string)x.Attribute(attrName), true);
            }

            /// <summary>
            /// Convert the underlying value to the type of the enumeration.
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="Value"></param>
            /// <returns></returns>
            public static T ToObject<T>(object Value)
                where T : struct
            {
                return (T)System.Enum.ToObject(typeof(T), Value);
            }

            /// <summary>
            /// Convenience to parse an enum value from an XElement attribute.
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="x"></param>
            /// <param name="attrName"></param>
            /// <param name="Value"></param>
            /// <returns></returns>
            public static bool TryParse<T>(XElement x, string attrName, out T Value)
                where T : struct
            {
                if (x == null)
                {
                    Value = default(T);
                    return false;
                }
                else
                    return TryParse<T>((string)x.Attribute(attrName), out Value);
            }

            public static bool TryParse<T>(string Name, out T Value)
                where T : struct
            {
                if (Name == null)
                {
                    Value = default(T);
                    return false;
                }
                else
                {
                    // get the list of names and check it, rather than using a try-catch, which becomes irritating when debugging
                    string[] names = GetNames<T>().Select(s => s.ToLower()).ToArray();
                    if (names.Contains(Name.ToLower()))
                    {
                        Value = Parse<T>(Name);
                        return true;
                    }
                    else
                    {
                        Value = default(T);
                        return false;
                    }
                }
            }

            /// <summary>
            /// Returns true if the specified name is an enum value of the specified type.
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="Name"></param>
            /// <returns></returns>
            public static bool IsValue<T>(string Name)
                where T : struct
            {
                T val;
                return TryParse<T>(Name, out val);
            }

            /// <summary>
            /// Returns true if the specified value is one of the enumeration values of the specified type.
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="Value"></param>
            /// <returns></returns>
            public static bool IsDefined<T>(object Value)
                where T : struct
            {
                return System.Enum.IsDefined(typeof(T), Value);
            }
        }
    }
}
